package com.bbdjt.weixin.web.action.patient;


import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Patient;
import com.bbdjt.weixin.model.PatientQueryModel;
import com.bbdjt.weixin.service.PatientService;
import com.github.pagehelper.PageInfo;

/**
 * @项目名称: bbdjt
 * @类名称: PatientAction
 * @类描述: Patient表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:43
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class PatientAction extends BaseAction {
	
	@Autowired
	private PatientService patientService;

	private String page_list = "jsp/patient/patientindex";

	private String redirect_list = "redirect:r_patient";
	
	private static Logger logger = Logger.getLogger(PatientAction.class);
	
	
	// 分页查询
	@RequestMapping(value = "r_patient")
	public ModelAndView getListPage(PatientQueryModel patient) {
		ModelAndView result = new ModelAndView(page_list);
		patient.setOrderBy("id asc");
		List<Patient> patientList = patientService.selectByPatient(patient);
		result.addObject("pageInfo", new PageInfo<Patient>(patientList));
		result.addObject("patient", patient);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_patientXml")
	public ModelAndView getListPageXml(PatientQueryModel patient) {
		ModelAndView result = new ModelAndView(page_list);
		patient.setOrderBy("id asc");
		PageInfo<PatientQueryModel> pageInfo = patientService.selectByPatientQueryModel(patient);
		List<PatientQueryModel> patientList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<PatientQueryModel>(patientList));
		result.addObject("patient", patient);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_patientJson", produces = "application/json")
	public ModelAndView pageInfo(PatientQueryModel patient) {
		ModelAndView result = new ModelAndView();
		PageInfo<PatientQueryModel> pageInfo = patientService.selectByPatientQueryModel(patient);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_patientLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(PatientQueryModel patient) {
		patient.setOrderBy(patient.getSortname(),patient.getSortorder());
		PageInfo<PatientQueryModel> pageInfo = patientService.selectByPatientQueryModel(patient);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_patient")
	public ModelAndView add(Patient patient) {
		ModelAndView result = new ModelAndView();
		if (patient.getId() != null) {
			patient = patientService.selectByKey(patient.getId());
		}
		result.addObject("patient", patient);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_patient")
	public ModelAndView save(Patient patient) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (patient.getId() != null) {
			patientService.updateNotNull(patient);
		} else {
			// 会保存null 值
			patientService.insertSelective(patient);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_patient")
	public String delete(Integer id) {
		patientService.delete(id);
		return redirect_list;
	}
	
	/**
	 * 患者提交入组
	 */
	@ResponseBody
	@RequestMapping(value = "addPatient", produces="application/json;charset=UTF-8")
	public String addPatient(Patient patient){
		Patient patient1 = new Patient();
		if(patient.getMobile() != null && patient.getDoctorCode() != null){
			patient1 = patientService.selectPatient(patient);
			if(patient1 != null){
				return super.writeOK(patient1);
			}else{
				patient.setCreateDate(new Date());
				patientService.insertSelective(patient);
				return super.writeOK(patient);
			}
		}else{
			logger.info("手机号或医生码为空:" + patient.getMobile() + "," + patient.getDoctorCode());
			return super.lackError();
		}
	}
}